import request from '@/common/request.js'	

/**
 * 登录
 */
export function login(data){
	return new Promise(function(resolve, reject) {
		request({
			url: "api/login",
			method: "post",
			data
		}).then(res => {
			return resolve(res)
		})
	})		
}
/**
 * 退出
 */
export function logout(){
	return new Promise(function(resolve, reject) {
		request({
			url: "api/logout",
			method: "GET"
		}).then(res => {
			return resolve(res)
		})
	})		
}
/**
 * 修改密码
 */
export function setpwd(data){
	return new Promise(function(resolve, reject) {
		request({
			url: "api/setpwd",
			method: "post",
			data
		}).then(res => {
			return resolve(res)
		})
	})		
}
/**
 * 获取用户信息
 */
export function userinfo(data){
	return new Promise(function(resolve, reject) {
		request({
			url: "api/userinfo",
			method: "get",
			data
		}).then(res => {
			return resolve(res)
		})
	})		
}

/**
 * 上传
 */
export function upload(data){
	return new Promise(function(resolve, reject) {
		request({
			url: "api/upload",
			method: "post",
			data
		}).then(res => {
			return resolve(res)
		})
	})		
}

/**
 * 发送验证码
 */
export function sendSmsCode(data){
	return new Promise(function(resolve, reject) {
		request({
			url: "api/smsSend",
			method: "post",
			data
		}).then(res => {
			return resolve(res)
		})
	})		
}

/**
 * 广告
 */
export function getAd(data){
	return new Promise(function(resolve, reject) {
		request({
			url: "api/ad",
			method: "get",
			data
		}).then(res => {
			return resolve(res)
		})
	})		
}
/**
 * 标签
 */
export function getLabel(data){
	return new Promise(function(resolve, reject) {
		request({
			url: "api/label",
			method: "get",
			data
		}).then(res => {
			return resolve(res)
		})
	})		
}

/**
 * 系统配置
 */
export function getSystemConfig(data){
	return new Promise(function(resolve, reject) {
		request({
			url: "api/systemConfig",
			method: "get",
			data
		}).then(res => {
			return resolve(res)
		})
	})		
}
/**
 * 获取位置
 */
export function getAddressByLocation(){
	
	return new Promise(function(resolve, reject) {
		
		// let location = uni.getStorageSync("location_data")
		// if(location && location!=undefined) {
		// 	console.log("location2", location)
		// 	return resolve(location)
		// }
		
		uni.getLocation({ 
			type: 'gcj02',
			success: function (res) {
				console.log("res", res) 
				let params = 
				request({
					url: "api/mapAddress",
					method: "get", 
					data: {
						location: res.latitude+","+res.longitude
					}
				}).then(result => {  
					console.log("result", result) 
					if(result.code != '0') return
					let data = result.data
					let location = { 
						latitude: res.latitude,
						longitude: res.longitude,
						address: data.address,
						city: data.ad_info.city.replace("市","")
					}
					
					console.log("location2", location)
					// uni.setStorageSync("location_data", location)
					return resolve(location)
				})
			}
		});
		
	})		
}
